<?php
namespace app\admini\controller;
use think\Controller;
use think\Db;
use think\request;
use think\Session;
use app\admini\controller\Base;

class Index extends Base
{
	
    public function index()
    {
    	$mydata = 'fsdfsdfs';
    	file_put_contents('test.txt',$mydata);
    	$admini = db('auth_admini u')->field('u.*,g.id group_id,g.title')->join('auth_group_access a', 'u.id=a.uid')->join('auth_group g', 'a.group_id=g.id')->where('u.id',session('ADMIN.id'))->find();
		$this->assign('admini',$admini);
		
		return $this->fetch();
    }
	
	public function welcome()
	{
		return $this->fetch();
	}
	
	public function system()
	{
		return $this->fetch();
	}
	
	//学校简介
	public function about()
	{
		$res = db('gw_about')->field('content,add_time,keys',true)->where('pid',1)->select();
		$this->assign('data',$res);
		return $this->fetch();
	}
	public function about_updata()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			file_put_contents('test.txt',$data['content']);
			unset($data['file']);
			$res = db('gw_about')->update($data);
			if($res){
				if($data['pid'] == 1){
					return $this->toSuccess('/admini/index/about');
				}else{
					return $this->toSuccess('/admini/index/hotcourse');
				}
			}else{
				return $this->toError();
			}
		}else{
			$id = input('id');
			$res = db('gw_about')->where('id',$id)->find();
			$this->assign('data',$res);
			return $this->fetch();	
		}
		
	}
	
	//热门课程
	public function hotcourse()
	{
		$res = Db::name('gw_about')->where('pid',2)->select();
		$this->assign('data',$res);
		return $this->fetch();
	}
	
	
	//学院动态
	public function news()
	{
		$map = array();
		$keyword=input('keyword');
		$pid=input('pid');
		if ($keyword) {
			$map['a.title'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($pid) {
			$map['a.pid'] = $pid;
		}else{
			$map['a.pid'] = array(
				'in',
				'4,5'
			);
		}
		$this->assign('pid', $pid);
		
		$res = Db::name('gw_news')->alias('a')->field('a.*,w.cate_name')->join('gw_newscate w','a.pid = w.id')->where($map)->order('add_time desc')->paginate(5, false, get_query());
		$this->assign('data',$res);
		$type = Db::name('gw_newscate')->where('pid',1)->select();
		$this->assign('type',$type);
		return $this->fetch();
	}
	
	public function news_add()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			$data['add_time'] = now_time();
			unset($data['file']);
			$fid = $data['fid'];
			unset($data['fid']);
			$res = db('gw_news')->insert($data);
			if($res){
				if($fid == 1){
					return $this->toSuccess('/admini/index/news');
				}else if($fid == 2){
					return $this->toSuccess('/admini/index/job');
				}else{
					return $this->toSuccess('/admini/index/answers');
				}
			}else{
				return $this->toError();
			}
		}else{
			$type = Db::name('gw_newscate')->where('pid',input('fid'))->select();
			$this->assign('type',$type);
			$this->assign('fid',input('fid'));
			return $this->fetch();	
		}
		
	}
	public function news_updata()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$res = db('gw_news')->update($data);
			if($res){
				if($data['pid'] == 1){
					return $this->toSuccess('/admini/index/news');
				}else if($data['pid'] == 2){
					return $this->toSuccess('/admini/index/job');
				}else{
					return $this->toSuccess('/admini/index/answers');
				}
			}else{
				return $this->toError();
			}
		}else{
			$data = db('gw_news')->find(input('id'));
			$this->assign('data',$data);
			$type = Db::name('gw_newscate')->where('pid',input('pid'))->select();
			$this->assign('type',$type);
			return $this->fetch();	
		}
		
	}
	
	//学校简介
	public function alist()
	{
		$map = array();
		$keyword=input('keyword');
		$pid=input('pid');
		if ($keyword) {
			$map['title'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($pid) {
			$map['pid'] = $pid;
		}
		$this->assign('pid', $pid);
		
		$res = Db::name('gw_alist')->where($map)->order('add_time desc')->paginate(5, false, get_query());
		$this->assign('data',$res);
		return $this->fetch();
	}
	
	public function alist_add()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			$data['add_time'] = now_time();
			unset($data['file']);
			$res = db('gw_alist')->insert($data);
			if($res){
				return $this->toSuccess('/admini/index/alist');
			}else{
				return $this->toError();
			}
		}else{
			return $this->fetch();	
		}
		
	}
	public function alist_updata()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$res = db('gw_alist')->update($data);
			if($res){
				return $this->toSuccess('/admini/index/alist');
			}else{
				return $this->toError();
			}
		}else{
			$data = db('gw_alist')->find(input('id'));
			$this->assign('data',$data);
			return $this->fetch();	
		}
		
	}
	
	
	
	//学员就业
	public function job()
	{
		$map = array();
		$keyword=input('keyword');
		$pid=input('pid');
		if ($keyword) {
			$map['a.title'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($pid) {
			$map['a.pid'] = $pid;
		}else{
			$map['a.pid'] = array(
				'in',
				'6,7'
			);
		}
		$this->assign('pid', $pid);
		
		$res = Db::name('gw_news')->alias('a')->field('a.*,w.cate_name')->join('gw_newscate w','a.pid = w.id')->where($map)->paginate(5, false, get_query());
		$this->assign('data',$res);
		$type = Db::name('gw_newscate')->where('pid',2)->select();
		$this->assign('type',$type);
		return $this->fetch();
	}
	
	//招生问答
	public function answers()
	{
		$map = array();
		$keyword=input('keyword');
		$pid=input('pid');
		if ($keyword) {
			$map['a.title'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		if ($pid) {
			$map['a.pid'] = $pid;
		}else{
			$map['a.pid'] = array(
				'in',
				'8,9,10'
			);
		}
		$this->assign('pid', $pid);
		
		$res = Db::name('gw_news')->alias('a')->field('a.*,w.cate_name')->join('gw_newscate w','a.pid = w.id')->where($map)->paginate(5, false, get_query());
		$this->assign('data',$res);
		$type = Db::name('gw_newscate')->where('pid',3)->select();
		$this->assign('type',$type);
		return $this->fetch();
	}
	
	//通知公告
	public function message()
	{
		$map = array();
		$keyword=input('keyword');
		if ($keyword) {
			$map['title'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		$map['pid'] = 1;
		$res = Db::name('gw_message')->where($map)->paginate(5, false, get_query());
		$this->assign('data',$res);
		return $this->fetch();
	}
	//在线报名
	public function baoming()
	{
		$map = array();
		$keyword=input('keyword');
		if ($keyword) {
			$map['title'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		$map['pid'] = 3;
		$res = Db::name('gw_message')->where($map)->paginate(5, false, get_query());
		$this->assign('data',$res);
		return $this->fetch();
	}
	//通知
	public function notice()
	{
		$map = array();
		$keyword=input('keyword');
		if ($keyword) {
			$map['title'] = array(
				'like',
				'%' . $keyword . '%'
			);
		}
		$this->assign('keyword', $keyword);
		$map['pid'] = 2;
		$res = Db::name('gw_message')->where($map)->paginate(5, false, get_query());
		$this->assign('data',$res);
		return $this->fetch();
	}
	public function notice_add()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			$data['add_time'] = now_time();
			$data['pid']=2;
			unset($data['file']);
			$res = db('gw_message')->insert($data);
			if($res){
				return $this->toSuccess('/admini/index/notice');
			}else{
				return $this->toError();
			}
		}else{
			return $this->fetch();	
		}
		
	}
	public function notice_updata()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$res = db('gw_message')->update($data);
			if($res){
				return $this->toSuccess('/admini/index/notice');
			}else{
				return $this->toError();
			}
		}else{
			$data = db('gw_message')->find(input('id'));
			$this->assign('data',$data);
			return $this->fetch();	
		}
		
	}
	
	//广告管理
	public function ads()
	{
		
		$res = Db::name('gw_ads')->order('sort asc')->select();
		$this->assign('data',$res);
		return $this->fetch();
	}
	
	public function ads_add()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$res = db('gw_ads')->insert($data);
			if($res){
				return $this->toSuccess('/admini/index/ads');
			}else{
				return $this->toError();
			}
		}else{
			return $this->fetch();	
		}
		
	}
	public function ads_updata()
	{
		if($this->request->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$res = db('gw_ads')->update($data);
			if($res){
				return $this->toSuccess('/admini/index/ads');
			}else{
				return $this->toError();
			}
		}else{
			$data = db('gw_ads')->find(input('id'));
			$this->assign('data',$data);
			return $this->fetch();	
		}
		
	}
	
	

}
